'''
Author: zhang
Date: 2025-03-31 10:33:10
Description: 
LastEditors: zhang
LastEditTime: 2025-03-31 17:48:23
'''
# --- generate_map_2d.py ---
import json
import math

map_width_pixels = 2048
map_height_pixels = 2304
tile_width = 50
# tile_height = 25 # 改为代表菱形的完整视觉高度
tile_visual_height = 25 # 明确表示菱形视觉高度
tile_vertical_spacing = tile_visual_height / 2.0 # 行间距变为 12.5

# 行数计算 (基于行间距)
num_rows = math.floor(map_height_pixels / tile_vertical_spacing) if tile_vertical_spacing > 0 else 0

# 列数计算 (逻辑不变，基于 tile_width)
max_col_even = math.floor(map_width_pixels / tile_width - 1) if tile_width > 0 else -1
max_col_odd = math.floor((map_width_pixels - (tile_width // 2)) / tile_width - 1) if tile_width > 0 else -1
max_col_index = min(max_col_even, max_col_odd)
num_cols = max_col_index + 1 if max_col_index >= 0 else 0

print(f"地图尺寸: {map_width_pixels}x{map_height_pixels} pixels")
print(f"格子尺寸: {tile_width}x{tile_visual_height} (宽度x菱形视觉高度)")
print(f"行间距: {tile_vertical_spacing}")
print(f"计算得到的地图格子数: {num_rows} 行 x {num_cols} 列")
print("-" * 20)

# --- 修改部分：初始化为二维数组结构 ---
map_grid = []  # 外层列表代表所有行
tile_id_counter = 1

for r in range(num_rows):
    current_row = []  # 内层列表代表当前行中的所有格子
    start_x = (r % 2) * (tile_width // 2)
    # 使用新的行间距计算 Y 坐标
    base_y = r * tile_vertical_spacing

    for c in range(num_cols):
        tile_x = start_x + c * tile_width
        tile_y = base_y # Y坐标基于行间距

        # 判断格子类型 (逻辑不变)
        is_outer_ring = (r < 2 or r >= num_rows - 2 or c < 2 or c >= num_cols - 2)
        tile_type = -1 if is_outer_ring else 0
        # 确保行列数足够大
        if num_rows < 4 or num_cols < 4:
             tile_type = -1 # 如果行列太少，所有格子都是外圈

        tile_data = {
            "id": tile_id_counter,
            "x": tile_x,
            "y": tile_y,
            "row": r,
            "column": c,
            "type": tile_type
        }
        current_row.append(tile_data) # 将格子数据添加到当前行
        tile_id_counter += 1

    map_grid.append(current_row) # 将完成的当前行添加到地图网格

# --- 输出 JSON (二维数组) ---
output_filename = "map_grid_scenario2_2d.json" # 使用新文件名表示二维结构
try:
    with open(output_filename, "w", encoding="utf-8") as f:
        # 直接将二维列表 map_grid 写入 JSON
        json.dump(map_grid, f, ensure_ascii=False, indent=2) # 使用 indent=2 提高可读性
    print(f"地图JSON数据 (二维数组) 已成功保存到文件: {output_filename}")
    # 计算总格子数
    total_tiles = num_rows * num_cols if num_rows > 0 and num_cols > 0 else 0
    print(f"共生成 {total_tiles} 个格子数据 ({num_rows} 行 x {num_cols} 列)。")
except IOError as e:
    print(f"错误：无法写入文件 {output_filename}。错误信息: {e}")
except Exception as e: # 捕获其他可能的错误
    print(f"发生意外错误: {e}")